package com.yichen.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import imusic4j.interfaces.Cate;

/**
 * 分类歌曲和歌手的工具类
 * 
 * @author yangyong
 * 
 */
public class ActorsUtils {

	Cate c = new Cate();

	/**
	 * 查询出歌手的分类
	 * 
	 * @return
	 */
	public List<Map<String, String>> getAcotr_TypeList() {
		// // TODO Auto-generated method stub
		List<Map<String, String>> re_list = new ArrayList<Map<String, String>>();

		String json_str = c.queryCateList();

		JSONObject json;
		try {
			json = new JSONObject(json_str);
			JSONObject queryBillboardListResponse = json
					.getJSONObject("queryCateLstResponse");
			JSONArray json_base = queryBillboardListResponse
					.getJSONArray("items");
			// for (int i = 0; i < json_base.length(); i++) {
			for (int i = 0; i < json_base.length() - 1; i++) {
				JSONObject jj = (JSONObject) json_base.get(i);
				JSONArray json_base_1 = jj.getJSONArray("childrenCateItem");
				for (int j = 0; j < json_base_1.length(); j++) {
					JSONObject jq = (JSONObject) json_base_1.get(j);
					if (!jq.getString("id").equals("gts")) {
						JSONArray json_base_2 = jq
								.getJSONArray("childrenCateItem");
						Log.e("size", json_base_2.length() + "");

						for (int q = 0; q < json_base_2.length(); q++)
							Log.v("tt",
									json_base_2.getJSONObject(q).getString(
											"des"));
					} else {
						jq.getString("des");
						// JSONArray json_base_2 = jq
						// .getJSONArray("childrenCateItem");
					}
				}
			}

			JSONObject jj = (JSONObject) json_base.get(2);
			if (jj.getString("id").equals("Artist")) {
				JSONArray json_base_1 = jj.getJSONArray("childrenCateItem");
				for (int j = 0; j < json_base_1.length(); j++) {
					JSONObject jj_1 = (JSONObject) json_base_1.get(j);
					String actor_type = jj_1.getString("des");
					String actor_type_id = jj_1.getString("id");
					Map<String, String> re_map = new HashMap<String, String>();

					re_map.put("actor_TYPE", actor_type);
					re_map.put("actor_TYPE_ID", actor_type_id);

					Log.v("actor_type", actor_type + actor_type_id);
					re_list.add(re_map);

				}

			} else {
				System.out.println("找不到");

			}
			// }
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return re_list;

	}

	/**
	 * 根据歌手的分类查出歌手
	 * 
	 * @return
	 */
	public List<Map<String, String>> getActorsByType(String cate_id) {
		// TODO Auto-generated method stub
		List<Map<String, String>> re_list = new ArrayList<Map<String, String>>();
		// String cateID = "1801-1802-1854";
		int startNum = 1;
		int maxNum = 10;
		String order = "id";

		String json_str = c.queryActorsInfo(cate_id, startNum, maxNum, order);
		// System.out.println(json_str);
		JSONObject json;
		try {
			json = new JSONObject(json_str);
			JSONObject queryBillboardListResponse = json
					.getJSONObject("queryActorsResponse");
			JSONArray json_base = queryBillboardListResponse
					.getJSONArray("items");
			for (int j = 0; j < json_base.length(); j++) {
				JSONObject jj = (JSONObject) json_base.get(j);
				// System.out.println("记录：" + jj);

				String actor_name = jj.getString("name");
				String actor_id = jj.getString("id");
				Map<String, String> re_map = new HashMap<String, String>();

				re_map.put("actor_NAME", actor_name);
				re_map.put("actor_ID", actor_id);

				Log.v("actor_type_actor", actor_name + actor_id);
				re_list.add(re_map);

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return re_list;

	}
}
